package com.hxzy.common.exception;

import com.hxzy.common.enums.AckCode;
import com.hxzy.common.vo.resp.R;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 功能描述: Springmvc统一异常处理的操作(500错误)
 *
 * @author tonneyyy
 */
@RestControllerAdvice
public class SpringMvcGlobalException {

    /**
     * hibernate-validator数据验证不成功的错误处理方式
     * @param e
     * @return
     */
    @ExceptionHandler(value = BindException.class)
    public R<Object> bindException(BindException e){
       String msg=e.getAllErrors().get(0).getDefaultMessage();
        //自定义返回结果
        R<Object> build = R.build(AckCode.SYSTEM_PARAM_FAIL);
        build.setMsg(msg);
        return build;
    }
}
